package org.dyndns.opendemogroup.hadoopoptimizer;

import java.io.IOException;
import java.util.Iterator;

import org.apache.hadoop.io.Text;
import org.apache.hadoop.io.WritableComparable;
import org.apache.hadoop.mapred.MapReduceBase;
import org.apache.hadoop.mapred.OutputCollector;
import org.apache.hadoop.mapred.Reducer;
import org.apache.hadoop.mapred.Reporter;

public class DistributedRunReducer extends MapReduceBase implements Reducer
{
	private final static Text outputValue = new Text ( );

	/**
	 * Emits &lt;bestFitness, genome&gt; pairs representing the results of the
	 * optimizer runs.
	 * 
	 * @param key
	 *        Represents the fitness achieved.
	 * @param values
	 *        Represents the genomes that computed a fitness of <i>key</i>.
	 * @see Reducer#reduce(WritableComparable, Iterator, OutputCollector,
	 *      Reporter)
	 */
	@SuppressWarnings("unchecked")
	public void reduce ( WritableComparable _key, Iterator values,
			OutputCollector output, Reporter reporter ) throws IOException
	{
		while ( values.hasNext ( ) )
		{
			outputValue.set ( values.next ( ).toString ( ) );
			output.collect ( _key, outputValue );
		}
	}

}
